﻿from django import template
register=template.Library()
from django.utils.safestring import mark_safe

@register.simple_tag
def page(page_obj):
    pre_page='<li class="page-pre"><a href="/user/?page=%s">&lt;</a></li>'%(page_obj.number-1)
    pre_page_dis='<li class="page-pre disabled"><a>&lt;</a></li>'
    next_page='<li class="page-next"> <a href="/user/?page=%s">&gt;</a></li>'%(page_obj.number+1)
    next_page_dis='<li class="page-next disabled"> <a>&gt;</a></li>'
    page_eml=[]
    if page_obj.has_previous():
        page_eml.append(pre_page)
    else:
        page_eml.append(pre_page_dis)
    per_page=3
    if page_obj.paginator.num_pages < per_page*2+1:
        start_page=1
        end_page=page_obj.paginator.num_pages+1
    else:
        if page_obj.number + per_page > page_obj.paginator.num_pages:
            end_page=page_obj.paginator.num_pages+1
            start_page=page_obj.paginator.num_pages-per_page*2
        else:
            if page_obj.number -per_page <= 0:
                start_page=1
                end_page=per_page*2+2
            else:
                start_page=page_obj.number-per_page
                end_page=page_obj.number+per_page+1
    for i in range(start_page,end_page):
        if page_obj.number == i:
            eml='<li class="page-next active"> <a href="/user/?page=%s">%s</a></li>'%(i,i)
        else:
            eml='<li class="page-next "> <a href="/user/?page=%s">%s</a></li>'%(i,i)
        page_eml.append(eml)
    if page_obj.has_next():
        page_eml.append(next_page)
    else:
        page_eml.append(next_page_dis)
    go="""
    <div class="col-sm-2 table-toolbar-right">
        <input type="text"    id="go">
        <button onclick="return go()" class="btn btn-purple"><i class="demo-pli-add icon-fw"></i>GO</button>
        <a class="btn btn-purple" href="/user/?page=%s">末页</a>
    </div>
    """%page_obj.paginator.num_pages
    page_eml.append(go)
    res=''.join(page_eml)
    return mark_safe(res)
